package com.ithiem.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.github.pagehelper.PageHelper;
import com.itheima.dubbo.api.UserInfoApi;
import com.itheima.model.domain.UserInfo;
import com.itheima.model.vo.PageResult;
import com.ithiem.dubbo.mappers.UserInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    /*根据用户ids查询用户信息，有userinfo就添加添加查询（分页）*/
    /*@Override//sql语句
    public PageResult findByIds(List<Long> ids, UserInfo userInfo, Integer page, Integer pagesize) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.in(UserInfo::getId, ids);
        if (null != userInfo) {
            if (null != userInfo.getAge()) {
                lqw.lt(UserInfo::getAge, userInfo.getAge());
            }
            if (!StringUtils.isEmpty(userInfo.getGender())) {
                lqw.eq(UserInfo::getGender, userInfo.getGender());
            }
            if(!StringUtils.isEmpty(userInfo.getNickname())){
                lqw.like(UserInfo::getNickname,userInfo.getNickname());
            }
        }
        Page<UserInfo> userInfoPage = new Page<>(page, pagesize);
        IPage<UserInfo> userInfos = userInfoMapper.selectPageByIds(userInfoPage, userInfo.getAge(), userInfo.getGender(), userInfo.getNickname(), ids);
        PageResult pageInfo = new PageResult(page, pagesize, Math.toIntExact(userInfos.getTotal()), userInfos.getRecords());
        return pageInfo;
    }*/
    @Override
    public PageResult findByIds(List<Long> ids, UserInfo userInfo, Integer page, Integer pagesize) {
        if(ids==null||ids.size()==0){
            return new PageResult();
        }
        Page<UserInfo> userInfoPage = new Page<>(page, pagesize);
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.lt(userInfo.getAge()!=null,UserInfo::getAge, userInfo.getAge());
        lqw.eq(!StringUtils.isEmpty(userInfo.getGender()),UserInfo::getGender,userInfo.getGender());
        lqw.like(userInfo.getNickname()!=null,UserInfo::getNickname,userInfo.getNickname());
        lqw.in(UserInfo::getId,ids);
        IPage<UserInfo> userInfos = userInfoMapper.selectPage(userInfoPage,lqw);
        PageResult pageInfo = new PageResult(page, pagesize, userInfos.getTotal(), userInfos.getRecords());//count数不对
        return pageInfo;
    }
/*    @Override
    public PageResult findByIds(List<Long> ids, UserInfo userInfo, Integer page, Integer pagesize) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.in(UserInfo::getId, ids);
        if (null != userInfo) {
            if (null != userInfo.getAge()) {
                lqw.lt(UserInfo::getAge, userInfo.getAge());
            }
            if (!StringUtils.isEmpty(userInfo.getGender())) {
                lqw.eq(UserInfo::getGender, userInfo.getGender());
            }
            if (!StringUtils.isEmpty(userInfo.getNickname())) {
                lqw.like(UserInfo::getNickname, userInfo.getNickname());
            }
        }
        PageHelper.startPage(page, pagesize);//分页查询
        List<UserInfo> userInfos = userInfoMapper.selectList(lqw);
        PageResult pageInfo = new PageResult(userInfos);
        return pageInfo;
    }*/

    /*推荐用户列表查询(不分页)
     * 筛选年龄，性别
     * */
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo userInfo) {
        if(CollUtil.isEmpty(userIds)&&null==userInfo){
            return new HashMap<>();
        }
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.in(userIds!=null,UserInfo::getId, userIds);
        if (null != userInfo) {
            if (null != userInfo.getAge()) {
                lqw.lt(UserInfo::getAge, userInfo.getAge());
            }
            if (!StringUtils.isEmpty(userInfo.getGender())) {
                lqw.eq(UserInfo::getGender, userInfo.getGender());
            }
            if (!StringUtils.isEmpty(userInfo.getNickname())) {
                lqw.like(UserInfo::getNickname, userInfo.getNickname());
            }
        }
        List<UserInfo> userInfos = userInfoMapper.selectList(lqw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(userInfos, "id");
        return map;
    }

    @Override
    public void updateSoulLevel(UserInfo userInfo) {
        //修改用户灵魂等级
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        UserInfo userInfo1 = userInfoMapper.selectById(userInfo.getId());
        if (userInfo1.getSoulLevel().equals(userInfo.getSoulLevel())){
            userInfo.setSoulLevel((Integer.valueOf(userInfo.getSoulLevel())+1)+"");
            userInfoMapper.updateById(userInfo);//如果用户等级等于这次答题等级，就更新等级
        }
    }

    @Override
    public Page<UserInfo> users(Integer page, Integer pagesize, Long id, String nickname, String city) {
        Page<UserInfo> pages = new Page(page, pagesize);
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(org.apache.commons.lang.StringUtils.isNotEmpty(nickname), "nickname", nickname)
                .like(org.apache.commons.lang.StringUtils.isNotEmpty(city), "city", city)
                .eq(id != null, "id", id);
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(pages, queryWrapper);
        return userInfoPage;
    }
}
